# -*- coding: utf-8 -*-
import os
import time
import re
import subprocess
import shutil 
import hashlib
import threading
import smtplib
import  copy
import logging
import requests
import hashlib
import  os

class updateAndCompile():
    def __init__(self):
        self.header={'Api-Key':'2VjJxSU1WX8QSScTxFwfVGt6FcNVkZMr'}
        self.url = 'http://erp.openluat.com/api/open/product_software'


    def PubVer(self,path_name,email,project,project_type,code_path):
        soft_path = "G:\\8910_V1.3\\hex\\AirM2M_720U_V301707_LTE_AT_TMP.zip"
        hd  = open(soft_path,"rb")
        soft = hd.read()
        hd.close()
        soft_md5  =   hashlib.md5(soft).hexdigest()

        soft_map_path = "G:\\8910_V1.3\\hex\\AirM2M_720U_V301707_LTE_AT_TMP_MAP.zip"
        hd  = open(soft_map_path,"rb")
        soft_map = hd.read()
        hd.close()
        soft_map_md5  =   hashlib.md5(soft_map).hexdigest()

        soft_ota_path = "G:\\8910_V1.3\\hex\\AirM2M_720U_V301707_LTE_AT_TMP\\AirM2M_720U_V301707_LTE_AT_TMP.dfota.bin"
        hd  = open(soft_ota_path,"rb")
        data_ota = hd.read()
        hd.close()
        data_ota_md5  =   hashlib.md5(data_ota).hexdigest()
        postdatas = {'name': path_name ,
             "info":"自动测试说明",
             "info_detail":"自动测试详细说明",
             "info_public":"自动测试详细说明",
             "usage":"正式版本",
             "code_path":code_path,
             "software_type":"8910",
             "software_type_2":project,
             "software_type_3":project_type,
             "product_ids":"1243,1335,1434",
             "software_md5":soft_md5,
             "bin_md5":data_ota_md5,
             "email":email,
             "map_md5":soft_map_md5,
             "test_tool_md5":None,
             "self_test_report_md5":None
             }
        files  = {
            "software":("AirM2M_720U_V301707_LTE_AT_TMP.zip",soft),
            "bin": ("AirM2M_720U_V301707_LTE_AT_TMP.dfota.bin",data_ota) ,
            "map": ("AirM2M_720U_V301707_LTE_AT_TMP_MAP.zip",soft_map)
        }

        requestRes = requests.post(self.url, data=postdatas,headers=self.header,files = files)
        print(requestRes.status_code,requestRes.text.encode('utf-8').decode('gbk'))

        pass
